package com.company.common.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtils
{
    public static final String DATE_FORMAT_Y_M_D = "yyyy-MM-dd";
    public static final String DATE_FORMAT_YMD = "yyyyMMdd";
    public static final String DATE_FORMAT_Y_M_D_H_M_S = "yyyy-MM-dd HH:mm:ss";
    public static final String DATE_FORMAT_YMDHMS = "yyyyMMddHHmmss";

    /**
     * 日期格式转换成字符串
     */
    public static String date_to_str(Date date,String pattern)
    {
        SimpleDateFormat format = new SimpleDateFormat(pattern);
        return format.format(date);
    }

    /**
     * 字符串转换成日期
     */
    public static Date str_to_date (String dateStr,String pattern) throws ParseException
    {
        SimpleDateFormat format = new SimpleDateFormat(pattern);
        return format.parse(dateStr);
    }

    /**
     * 当前时间
     */
    public static Date getNowDate()
    {
    	Calendar calendar = Calendar.getInstance();
    	calendar.setTime(new Date());
    	return calendar.getTime();
    }

    /**
     * 日期 00：00：00
     */
    public static Date getDayMin(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(null == date?getNowDate():date);
        calendar.set(Calendar.HOUR_OF_DAY, 00);
        calendar.set(Calendar.MINUTE, 00);
        calendar.set(Calendar.SECOND, 00);
        calendar.set(Calendar.MILLISECOND, 0);

        return new Date(calendar.getTime().getTime());
    }

    /**
     * 日期 23：59：59
     */
	public static Date getDayMax(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(null == date?getNowDate():date);
		calendar.set(Calendar.HOUR_OF_DAY, 23);
		calendar.set(Calendar.MINUTE, 59);
		calendar.set(Calendar.SECOND, 59);
		calendar.set(Calendar.MILLISECOND, 0);

		return new Date(calendar.getTime().getTime());
	}

    /**
     * 正数增加  负数减少天
     */
	public static Date addDay(Date date,int day)
	{
	    Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.DAY_OF_MONTH,calendar.get(Calendar.DAY_OF_MONTH)+ day);
        return calendar.getTime();
	}

    /**
     * 正数增加  负数减少年
     */
    public static Date addYear(Date date,int year)
    {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.YEAR,calendar.get(Calendar.YEAR)+ year);
        return calendar.getTime();
    }

    /**
     * 获取当月第一天
     */
    public static String getMonthStartDay(Date date,int month) {
        Calendar cale = Calendar.getInstance();
        cale.setTime(date);
        // 获取当月第一天
        SimpleDateFormat format = new SimpleDateFormat(DATE_FORMAT_Y_M_D_H_M_S);
        cale.set(Calendar.DAY_OF_MONTH, 1);
        cale.set(Calendar.HOUR_OF_DAY, 00);
        cale.set(Calendar.MINUTE, 00);
        cale.set(Calendar.SECOND, 00);
        cale.set(Calendar.MILLISECOND, 0);

        return format.format(cale.getTime());

    }

    public static String getMonthEndDay(Date date,int month) {
        Calendar cale = Calendar.getInstance();
        cale.setTime(date);
        // 获取前月的最后一天
       SimpleDateFormat format = new SimpleDateFormat(DATE_FORMAT_Y_M_D_H_M_S);
        cale.set(Calendar.DAY_OF_MONTH, cale.getActualMaximum(Calendar.DAY_OF_MONTH));
        cale.set(Calendar.HOUR_OF_DAY, 23);
        cale.set(Calendar.MINUTE, 59);
        cale.set(Calendar.SECOND, 59);
        cale.set(Calendar.MILLISECOND, 0);
        return format.format(cale.getTime());
    }
}
